package com.sky.directdeal.activity;

import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.sky.directdeal.R;
import com.sky.directdeal.adapter.SubCategoryListAdapter;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.listener.ServiceResponseListener;
import com.sky.directdeal.manager.ServiceManager;
import com.sky.local_ads.sub_category.SubCategoryList;
import com.sky.local_ads.sub_category.SubCategoryResponse;


public class SubCategoryListActivity extends BaseActivity {
	private boolean isService=false;
	private SubCategoryListAdapter subCategoryAdapter;
	private ListView subCategoryListview;
	private int catId,subCatId=-1;
	private String catName=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sub_category_list);
		setHomeUp();
		initUi();
		getBundleData(this);
	}

	private void initUi(){
		subCategoryListview=(ListView)findViewById(R.id.listViewSubCategories);
		subCategoryListview.setOnItemClickListener(subCategoryListClickListner);
	}
	
	OnItemClickListener subCategoryListClickListner=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			
			SubCategoryList subCategoryList=(SubCategoryList)arg0.getItemAtPosition(arg2);
			subCatId=subCategoryList.getSubCategoryId();
			Intent businessListIntent=new Intent(SubCategoryListActivity.this,BusinessListActivity.class);
			businessListIntent.putExtra(Constants.INTENT_CATEGORYY_ID,catId);
			businessListIntent.putExtra(Constants.INTENT_CATEGORYY_NAME,catName);
			businessListIntent.putExtra(Constants.INTENT_SUB_CATEGORYY_ID,subCatId);
			startActivity(businessListIntent);
			
		}
	};
	
	private void getBundleData(Context context) {
		if (getIntent().getExtras() != null
				&& getIntent().hasExtra(Constants.INTENT_CATEGORYY_ID)) {
			
			catId = getIntent().getIntExtra(Constants.INTENT_CATEGORYY_ID,0);
			
			catName = getIntent().getStringExtra(
					Constants.INTENT_CATEGORYY_NAME);
			
			setHeaderTitle(catName);
			
			if (catId > 0) {
				
				if (!isService) {
					isService = true;
					mainCategoryHandler.sendEmptyMessage(Constants.SHOW_PROGRESS_DIALOG);
					ServiceManager.getSubCategoriesList(
							SubCategoryListActivity.this,
							String.valueOf(catId), subCategoryResponseListener);
				}
			}

		}
	}
	
	ServiceResponseListener<SubCategoryResponse> subCategoryResponseListener=new ServiceResponseListener<SubCategoryResponse>() {
		
		@Override
		public void onSuccess(SubCategoryResponse response) {
			isService=false;
			mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
			if(response!=null && response.getSubcategoryList()!=null){
				List<SubCategoryList> subCategoryList=response.getSubcategoryList();
				subCategoryAdapter=new SubCategoryListAdapter(SubCategoryListActivity.this, subCategoryList);
				subCategoryListview.setAdapter(subCategoryAdapter);
				
			}else{
				showAlert(SubCategoryListActivity.this, getString(R.string.alert), getString(R.string.no_sub_categories_found), getString(R.string.ok_button), positiveListener, null, null);
			}
			
			
		}
		
		@Override
		public void onFailure(Throwable throwable, String errorResponse) {
			isService=false;
			mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
			showAlert(SubCategoryListActivity.this, getString(R.string.server_failed), getString(R.string.server_unavailable), getString(R.string.ok_button), positiveListener, null, null);
		}
	};
	
	public DialogInterface.OnClickListener positiveListener=new DialogInterface.OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			finish();
		}
	};
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
